﻿namespace SilverSea.Databases
{
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Threading;

    public class AccessConnection : IConnection
    {
        private bool bool_0;
        public OleDbConnection conn;
        private int int_0;
        private string string_0;
        private string string_1;
        private string string_2;
        private string string_3;

        public event DatabaseConnectionErrorEventHandler DatabaseConnectionErrorEvent;

        public AccessConnection()
        {
            this.string_0 = "Microsoft.Jet.OLEDB.4.0";
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.conn = new OleDbConnection();
        }

        public AccessConnection(string databaseName)
        {
            this.string_0 = "Microsoft.Jet.OLEDB.4.0";
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.conn = new OleDbConnection();
            this.string_1 = databaseName;
        }

        public AccessConnection(string databaseName, string password)
        {
            this.string_0 = "Microsoft.Jet.OLEDB.4.0";
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.conn = new OleDbConnection();
            this.string_1 = databaseName;
            this.string_3 = password;
        }

        public void Close()
        {
            if (this.conn.State == ConnectionState.Open)
            {
                this.conn.Close();
                this.conn.Dispose();
            }
        }

        public bool ExecuteCommand(string commandString)
        {
            OleDbCommand command = new OleDbCommand(commandString, this.conn);
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public bool ExecuteCommand(string commandString, string parameters, byte[] values)
        {
            OleDbCommand command = new OleDbCommand(commandString, this.conn);
            command.Parameters.Add(parameters, OleDbType.Binary);
            if (values != null)
            {
                command.Parameters[parameters].Value = values;
            }
            else
            {
                command.Parameters[parameters].Value = DBNull.Value;
            }
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public bool ExecuteCommand(string commandString, string parameter1, byte[] value1, string parameter2, byte[] value2)
        {
            OleDbCommand command = new OleDbCommand(commandString, this.conn);
            command.Parameters.Add(parameter1, OleDbType.Binary);
            if (value1 != null)
            {
                command.Parameters[parameter1].Value = value1;
            }
            else
            {
                command.Parameters[parameter1].Value = DBNull.Value;
            }
            command.Parameters.Add(parameter2, OleDbType.Binary);
            if (value2 != null)
            {
                command.Parameters[parameter2].Value = value2;
            }
            else
            {
                command.Parameters[parameter2].Value = DBNull.Value;
            }
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public string GetConnectionString()
        {
            if (this.int_0 == 0)
            {
                return ("Provider=" + this.string_0 + ";Data Source='" + this.string_1);
            }
            return ("Provider=" + this.string_0 + ";Data Source='" + this.string_1 + "';Persist Security Info=True;Jet OLEDB:Database Password=" + this.string_3);
        }

        public DataSet GetDataSet(string commandString)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter(commandString, this.conn);
            DataSet dataSet = new DataSet();
            try
            {
                adapter.Fill(dataSet);
                adapter.Dispose();
                return dataSet;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while get dataSet fom table: " + exception.Message + "\n" + commandString);
                }
            }
            return null;
        }

        public OleDbDataReader GetReader(string commandString)
        {
            OleDbCommand command = new OleDbCommand(commandString, this.conn);
            return command.ExecuteReader();
        }

        public DataTable GetTable(string commandString)
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter(commandString, this.conn);
            DataSet dataSet = new DataSet();
            try
            {
                adapter.Fill(dataSet);
                adapter.Dispose();
                return dataSet.Tables[0];
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while get data fom table: " + exception.Message + "\n" + commandString);
                }
            }
            return null;
        }

        public bool Open()
        {
            try
            {
                if (this.conn.State == ConnectionState.Open)
                {
                    this.conn.Close();
                }
                this.conn.ConnectionString = this.GetConnectionString();
                this.conn.Open();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent(exception.Message);
                }
                return false;
            }
        }

        public int Authentication
        {
            get
            {
                return this.int_0;
            }
            set
            {
                this.int_0 = value;
            }
        }

        public string DatabaseName
        {
            get
            {
                return this.string_1;
            }
            set
            {
                this.string_1 = value;
            }
        }

        public bool EnableMARS
        {
            get
            {
                return this.bool_0;
            }
            set
            {
                this.bool_0 = value;
            }
        }

        public string Password
        {
            get
            {
                return this.string_3;
            }
            set
            {
                this.string_3 = value;
            }
        }

        public string ServerName
        {
            get
            {
                return this.string_0;
            }
            set
            {
                this.string_0 = value;
            }
        }

        public ConnectionState State
        {
            get
            {
                return this.conn.State;
            }
        }

        public string UserName
        {
            get
            {
                return this.string_2;
            }
            set
            {
                this.string_2 = value;
            }
        }
    }
}

